package com.ocom.mqsimple.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.MqSimpleApiUrl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.GetNotsendRecordRequest;
import com.ocom.common.request.simple.MqTimerRequest;
import com.ocom.common.request.web.CommonPidVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mqsimple.business.unicom.impl.UnicomAccTypeProductServiceImpl;
import com.ocom.mqsimple.business.unicom.impl.UnicomProductServiceImpl;
import com.ocom.mqsimple.feign.PayhandleClientService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class UnicomController {

    private final Logger logger = LoggerFactory.getLogger(UnicomController.class);

    @Autowired
    private UnicomProductServiceImpl productService;
    @Autowired
    private PayhandleClientService payhandleClientService;

    @Autowired
    private UnicomAccTypeProductServiceImpl unicomAccTypeProductService;


    @PostMapping(MqSimpleApiUrl.URL_unicomMqConsEvent)
    public Result consEvent(@RequestBody MqTimerRequest request) {
        try {
            log.info("consEvent=====参数：【{}】", request);
            GetNotsendRecordRequest getNotsendRecordRequest = new GetNotsendRecordRequest();
            getNotsendRecordRequest.setCompanyId(request.getComId());
            getNotsendRecordRequest.setSize(request.getAutoSize());
            getNotsendRecordRequest.setConsStates(Arrays.asList(1,2));
            getNotsendRecordRequest.setPayState(Arrays.asList(1));
//            getNotsendRecordRequest.setPrefix();
            Result notsendRecord = payhandleClientService.getNotsendRecord(getNotsendRecordRequest);

            if (200 == notsendRecord.getCode() && null != notsendRecord.getData()) {
                List<EntityXfRecordProcess> getNosendRecordList = JSON.parseArray(JSON.toJSONString(notsendRecord.getData()), EntityXfRecordProcess.class);

                for (EntityXfRecordProcess recordProcess : getNosendRecordList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("comId", recordProcess.getComId());
                    jsonObject.put("orderNo", recordProcess.getOrderNo());
                    log.info("consEvent.handle=====参数：【{}】", jsonObject);
                    productService.handle(jsonObject.toJSONString());
                }
            }

        } catch (Exception e) {
            logger.error("发送mq失败  错误【{}】", e);
            return ResultUtil.error(ResultEnum.Err_4006);
        }
        return ResultUtil.success();
    }


    @PostMapping(MqSimpleApiUrl.URL_unicomMqUpdateAccType)
    public Result updateAccType(@RequestBody CommonPidVo request) {
        try {
            log.info("consEvent=====参数：【{}】", request);
            unicomAccTypeProductService.handle(JSON.toJSONString(request));
        } catch (Exception e) {
            logger.error("发送mq失败  错误【{}】", e);
            return ResultUtil.error(ResultEnum.Err_4006);
        }
        return ResultUtil.success();
    }

}
